/**
 * 层次分析法
 * */
public class AhpAlgorithm {
    private static int n = 5;//矩阵的阶数







    public static Double[] ahp(){
        Double[][] matrix = new Double[n][n];
        for(int i=0;i<n;i++){
            matrix[i][i]=1.0;//对角线元素为1.0
        }
        matrix[0][1] = 2.0;
        matrix[0][2] = 2.0;
        matrix[0][3] = 3.0;
        matrix[0][4] = 2.0;
        matrix[1][2] = 3.0;
        matrix[1][3] = 3.0;
        matrix[1][4] = 2.0;
        matrix[2][3] = 2.0;
        matrix[2][4] = 3.0;
        matrix[3][4] = 2.0;
        //根据输入值填写矩阵剩余项
        for (int i=n-1;i>=0;i--){
            for (int j=n-1;j>=0;j--){
                matrix[i][j]=1/matrix[j][i];
            }
        }
        Double[] column = new Double[n];
        for (int j=0;j<n;j++){
            for (int i=0;i<n;i++){
                if(column[j]!=null) {
                    column[j] = column[j] + matrix[i][j];
                }else{
                    column[j] = matrix[i][j];
                }
            }
        }

        //矩阵归一化

        Double[][] matrixColumn = new Double[n][n];
        for(int j=0;j<n;j++){
            for(int i=0;i<n;i++){
                matrixColumn[i][j] = matrix[i][j]/column[j];
            }
        }

        //获得行数组
        Double[] line = new Double[n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(line[i] != null){
                    line[i] = line[i] + matrixColumn[i][j];

                }else{
                    line[i] = matrixColumn[i][j];
                }
            }
        }

        //行归一化获得特征向量
        Double[] w = new Double[n];
        Double sum = 0.0;
        for (int i=0;i<n;i++){
            sum = sum+line[i];
        }
        for(int i=0;i<n;i++) {
            w[i] = line[i] / sum;//特征向量

        }
        Double[] bw = new Double[n];
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                if (bw[i] != null){
                    bw[i] = bw[i]+matrix[i][j]*w[j];
                }else{
                    bw[i] = matrix[i][j]*w[j];
                }
            }
        }
        Double sumR = 0.0;//最大特征跟R
        for(int i=0;i<n;i++){
            sumR = sumR+bw[i]/(n*w[i]);
        }
        Double ci = (sumR-n)/(n-1);//矩阵一致性指标
        System.out.println("计算出矩阵一致性指标"+ci+"\n");
        Double cr = ci/1.12;     //1.12为5阶矩阵的平均一致性指标
        if (cr>=0.1){
            System.out.println("矩阵未通过一致性检验");

        }
        return w;
    }

}
